<?php
namespace Home\Controller;
use Think\Controller;
class HomeController extends Controller{
    public function home(){
        //获得参数 signature nonce token timestamp echostr
		$nonce     = $_GET['nonce'];
		$token     = 'test';
		$timestamp = $_GET['timestamp'];
		$echostr   = $_GET['echostr'];
		$signature = $_GET['signature'];
		//形成数组，然后按字典序排序
		$array = array();
		$array = array($nonce, $timestamp, $token);
		sort($array);
		//拼接成字符串,sha1加密 ，然后与signature进行校验
		$str = sha1( implode( $array ) );
		if( $str  == $signature && $echostr ){
			//第一次接入weixin api接口的时候
			echo  $echostr;
			exit;
		}		
        $list = M('module_lists');
        $some['title']=array('like','%'.trim($_GET['title'].'%'));
        $some['drupal_version']=array('like','%'.trim($_GET['drupal_version'].'%'));
        $some['module_class']=array('like','%'.trim($_GET['module_class'].'%'));
        $some['status'] = 1;
        $count = $list ->where($some)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $lists = $list->where($some)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this -> assign('lists',$lists); 
        $this -> assign('page',$show);
        $this -> assign('count',$count);
        $this ->display();	
    }
    public function help(){
        $this ->display();
    }
    public function feedback(){
        $this ->display(); 
    }
    public function dofeedback(){
        require_once LIB_PATH. 'Custom/securimage/securimage.php';
        $img = new\ Securimage();
        $w = $_POST['captcha_token'];
        if( $img->check($w)==true){            
            $data = array();
            $data['title'] = trim($_POST['title']);
            $data['email'] = trim($_POST['email']);
            $data['body'] = trim($_POST['body']);
            $feedback = M('feedback');
            $feedback -> add($data);
            header("Location: ".U('Home/feedback2'));
        }else{
            $this -> error('验证码错误！',U('Home/register'));
        }
    }
    public function feedback2(){
        $this ->display(); 
    }
    public function add_module(){
        $this ->display(); 
    }
    public function doadd_module(){
        require_once LIB_PATH. 'Custom/securimage/securimage.php';
        $img = new\ Securimage();
        $w = $_POST['captcha_token'];
        if($img->check($w)==true){
            if("" != $_POST['title'] && "" != $_POST['drupal_version'] && "" != $_POST['module_class'] && "" != $_POST['body']){
                $module_lists = M('module_lists');
                $data = array();
                if(session('islogin')==true){
                $data['uid'] = session('id');
                }
                $data['title'] = trim($_POST['title']);
                $data['drupal_version'] = $_POST['drupal_version'];
                $data['module_class'] = $_POST['module_class'];
                $data['body'] = trim($_POST['body']);
                $data['create_time'] = time();
                $data['update_time'] = time();
                if($_FILES["image"]["type"] == "" || $_FILES["image"]["type"] == "image/gif" || $_FILES["image"]["type"] == "image/jpeg"|| $_FILES["image"]["type"] == "image/png" || $_FILES["image"]["type"] == "image/jpg" && $_FILES["image"]["size"] <= 2100000){
                    if($_FILES['image']['error'] == 0){
                       move_uploaded_file($_FILES['image']['tmp_name'],'THINK_PATH'.'/../Public/uploads/'.md5(time()).'.png');
                       $data['image'] = 'THINK_PATH'.'/../Public/uploads/'.md5(time()).'.png';
                    }
                }else{
                $this -> error('对不起，必须上传指定类型图片且大小小于2M。',U('Home/add_module'),2);
                }
                $module_lists ->add($data);
                $this -> success('添加成功',U('Home/add_module'));
            }else{
                 $this -> error('“版本”，“模块分类”，“模块描述”必须存在！',U('Home/add_module'),2);
            }
        }else{
            $this -> error('验证码错误！',U('Home/add_module'));
        }
    }
    public function moduleread(){
        $list = M('module_lists');
        $some = array();
        $some['id'] = $_GET['id'];
        $read = $list ->where($some) ->find();
        $now = time();
        $this -> assign('now',$now);
        $this -> assign('read',$read);
        $this -> display();
    }
    public function eredar(){
        $user = M('user');
        $lists = $user ->where('contribution>0')->order('contribution desc')->limit(8)->select();    
        $this -> assign('lists',$lists);
        //print_r($lists);exit;
        $this -> display();
    }
    public function label(){
        $counts = M('module_lists');
        $tmp = $counts->field('module_class')->group('module_class')->select(); 
        $num = array();
        foreach($tmp as $k=>$v){
            $num[$v['module_class']] = $counts->where($tmp[$k])->count();
        }
        $this -> assign('num',$num);
        $this ->display();
    }
    public function labelread(){
        $list = M('module_lists');
        $some = array();
        $some['module_class'] = $_GET['module_class'];
        $lists = $list -> where($some)->select();
        $w = $_GET['module_class'];
        $this -> assign('lists',$lists);
         $this -> assign('w',$w);
        $this ->display();
    }
    public function register(){
        $this ->display();
    }
    public function yonghu(){
        $this ->display();
    }
    public function sms(){
        require_once LIB_PATH. 'Custom/sms.php';
        $sms = new \sms( array('api_key' => 'ec99d556fda3e80fa1f3e42c30d3c9d6' , 'use_ssl' => FALSE ) );
            //send 单发接口
            function create_password(){  
                $randpwd = "";  
                for ($i = 0; $i < 6; $i++){  
                    $randpwd .= chr(mt_rand(48,57));  
                }  
                return $randpwd;  
            }
            $pwd = create_password();
            session('pwd',$pwd);
            $res = $sms->send( $_POST['tel'] , '欢迎注册Drupal社区，您的验证码为：'.session('pwd').'，1分钟内有效。【Drupal社区】');
            if( $res ){
                if( isset( $res['error'] ) &&  $res['error'] == 0 ){
                    $putout = array();
                    $putout['status']=0;
                    print json_encode($putout);
                    exit;
                }else{
                    $putout = array();
                    $putout['status']=1;
                    print json_encode($putout);
                    exit;
                }
            }else{
                $putout = array();
                $putout['status']=2;
                print json_encode($putout);
                exit;
                exit;
            }
            
            //deposit 余额查询
            /*$res = $sms->get_deposit();
            if( $res ){
                    if( isset( $res['error'] ) &&  $res['error'] == 0 ){
                            echo 'desposit:'.$res['deposit'];
                    }else{
                            echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];
                    }
            }else{
                    var_dump( $sms->last_error() );
            }
            exit;*/
    }
    public function doregister(){
        require_once LIB_PATH. 'Custom/securimage/securimage.php';
        $img = new\ Securimage();
        $w = $_POST['captcha_token'];
        if( $img->check($w)==true){            
            $data = array();
            $data['name'] = trim($_POST['name']);
            $data['email'] = trim($_POST['email']);
            $data['pass'] = trim($_POST['pass']);
            $data1 = trim($_POST['REemail']);
            $data2 = trim($_POST['REpass']);
            if($data['email']==$data1 && $data['pass']==$data2){
                $user = M('user');
                $user -> add($data);
                $this -> success('注册成功。',U('Home/yonghu'));
            }else{
                $this -> error('两次输入的email或密码不一样！',U('Home/register'));
            }   
        }else{
            $this -> error('验证码错误！',U('Home/register'));
        }
    }
    public function dologin(){
        if("" != $_POST['name'] && "" != $_POST['pass']){
            $User = M('User');
            $someone = array();
            $someone['name'] = trim($_POST['name']);
            $someone['pass'] = trim($_POST['pass']);
            $lists = $User->where($someone)->find();
            if(empty($lists)){
                $this->error('密码或账号错误',U('Home/register'),1);
            }else{
                session("id",$lists['id']);
                session("name",$lists['name']);
                session("pass",$lists['pass']);
                session("islogin",true);
                $this ->success("登录成功！",U('Home/home'));
            }
        }else{
            header("Location: ".U('Home/yonghu'));
        }
    }
    public function back(){
        session(null);
        header("Location: ".U('Home/home'));
    }
    public function captcha(){
        require_once LIB_PATH. 'Custom/securimage/securimage.php';
        $img = new\ Securimage();
        if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);
        $img->show();
    }
    public function test(){
        $this ->display();
    }
    function getuserdetail(){
    	$appid ="wx4db0b276df1d7ac0";
		$redirect_uri =urlencode("http://dynamic-ip.imwork.net/youdengkui/index.php/Home/Home/getuserinfo");
		$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		header("location:".$url);
	}
	function getuserinfo(){
		$appid ="wx4db0b276df1d7ac0";
		$appsecret ="5fc147f68d5f544ab09fea72f5d0f3a3";
		$code =$_GET['code'];
		$url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
		$ch = curl_init();
		//2.设置curl的参数
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		//3.采集
		$res = curl_exec($ch);
		//4.关闭
		curl_close($ch);
        $res =json_decode($res,ture);
		$access_token =$res['access_token'];
		$openid =$res['openid'];
		$url ="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN ";
		$ch = curl_init();
		//2.设置curl的参数
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		//3.采集
		$res = curl_exec($ch);
		//4.关闭
		curl_close($ch);
        $res =json_decode($res,ture);
        
        
	}
}